package com.cnyao.phot;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.Service;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class TelephonyManagerTest extends ListActivity implements
		OnClickListener {
	private Button showInfoButton;
	private TelephonyManager mTelephonyManager;
	
	ArrayList<String> SIMInfo_name;
	ArrayList<String> SIMInfo_value;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.telephonyshow);
		this.init();
		showInfoButton.setOnClickListener(this);
	}
	
	public void init(){
		showInfoButton=(Button)this.findViewById(R.id.btn1);
		mTelephonyManager=(TelephonyManager)this.getSystemService(Service.TELEPHONY_SERVICE);
		SIMInfo_name=new ArrayList<String>();
		SIMInfo_value=new ArrayList<String>();
		
		SIMInfo_name.add(this.getResources().getString(R.string.SIM_state));
		if(mTelephonyManager.getSimState()==TelephonyManager.SIM_STATE_READY){
			SIMInfo_value.add("SIM卡状态良好");
		}
		else if(mTelephonyManager.getSimState()==TelephonyManager.SIM_STATE_ABSENT){
			SIMInfo_value.add("SIM卡未插入");
		}
		else{
			SIMInfo_value.add("SIM卡状态未知");
		}
		
		SIMInfo_name.add(this.getResources().getString(R.string.SIM_Country));
		if(mTelephonyManager.getSimCountryIso().equals("")){
			SIMInfo_value.add("SIM卡国别无法取得");
		}
		else{
			SIMInfo_value.add(mTelephonyManager.getSimCountryIso());
		}
		
		SIMInfo_name.add(this.getResources().getString(R.string.SIM_LineNumber));
		SIMInfo_value.add(mTelephonyManager.getLine1Number());
		
		SIMInfo_name.add(this.getResources().getString(R.string.SIM_IMEI));
		SIMInfo_value.add(mTelephonyManager.getDeviceId());
		
		SIMInfo_name.add(this.getResources().getString(R.string.SIM_IMSI));
		SIMInfo_value.add(mTelephonyManager.getSubscriberId());
		
		SIMInfo_name.add(this.getResources().getString(R.string.SIM_MANYOU));
		SIMInfo_value.add(mTelephonyManager.isNetworkRoaming()?"漫游中":"非漫游");
		
		SIMInfo_name.add(this.getResources().getString(R.string.SIM_NetOper));
		SIMInfo_value.add(mTelephonyManager.getNetworkOperator());
		
		SIMInfo_name.add(this.getResources().getString(R.string.SIM_OperName));
		SIMInfo_value.add(mTelephonyManager.getNetworkOperatorName());
		
		String[] networkTypeArray = {"UNKNOWN", "GPRS", "EDGE", "UMTS", "CDMA", "EVDO 0", "EVDO A", "1xRTT", "HSDPA", "HSUPA", "HSPA"};
		SIMInfo_name.add(this.getResources().getString(R.string.SIM_NetType));
		SIMInfo_value.add(networkTypeArray[mTelephonyManager.getNetworkType()]);
		
		String[] phoneTypeArray = {"NONE", "GSM", "CDMA"};
		SIMInfo_name.add(this.getResources().getString(R.string.SIM_PhoneType));
		SIMInfo_value.add(phoneTypeArray[mTelephonyManager.getPhoneType()]);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn1:
			this.setListAdapter(new MyAdapter());
			LayoutAnimationController controller=
	        	AnimationUtils.loadLayoutAnimation(
	        		this, R.anim.list_layout_anim2);
			this.getListView().setLayoutAnimation(controller);
			break;
		}
	}

	class Holder{
		TextView name_TextView;
		TextView value_TextView;
	}

	class MyAdapter extends BaseAdapter {
		LayoutInflater inflater;
		Holder mHolder;
		
		@Override
		public int getCount() {
			return SIMInfo_name.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			inflater=LayoutInflater.from(TelephonyManagerTest.this);
			if(convertView==null){
				convertView=inflater.inflate(R.layout.item_layout, null);
				mHolder=new Holder();
				mHolder.name_TextView=(TextView)convertView.findViewById(R.id.item_TextView01);
				mHolder.value_TextView=(TextView)convertView.findViewById(R.id.item_TextView02);
				convertView.setTag(mHolder);
			}
			else{
				mHolder=(Holder)convertView.getTag();
			}
			mHolder.name_TextView.setText(SIMInfo_name.get(position));
			mHolder.value_TextView.setText(SIMInfo_value.get(position));
			return convertView;
		}
	}
}
